package api;

import donain.Product;
import com.alibaba.fastjson.JSON;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import exception.ProductException;
import jdk.nashorn.internal.objects.NativeDate;
import service.ProductService;
import utilts.Response;
import utilts.ResponseCode;

public class GetAllProductServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        System.out.println("1111");
//        第一步：获取所有商品
        List<Product> productList=null;
        ProductService productService=new ProductService();
        Response build=null;
        try{
            productList=productService.listAll();
        }catch (ProductException e){
            e.printStackTrace();
        }
       build = Response.build(ResponseCode.SUCCESS, productList);
        System.out.println(productList);
        System.out.println("--"+build.getData());
   
      resp.getWriter().write(JSON.toJSON(build).toString());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
